package com.ytzl.itrip.service.userinfo.impl;

import com.ytzl.itrip.dao.auto.userinfo.ItripUserMapper;
import com.ytzl.itrip.pojo.po.userinfo.ItripUser;
import com.ytzl.itrip.pojo.vo.userinfo.ItripUserVO;
import com.ytzl.itrip.service.mail.MailService;
import com.ytzl.itrip.service.userinfo.ItripUserService;
import com.ytzl.itrip.util.DataCacheUtil;
import com.ytzl.itrip.util.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Description: 本类用于业务实现类
 * Created by gengshu on 2019/12/23 11:56
 */
@Service
@Slf4j
public class ItripUserServiceImpl implements ItripUserService {
    @Autowired
    private ItripUserMapper itripUserMapper;
    @Autowired
    private MailService mailService;

    /**
     *  注册时用于查询当前email是否存在
     * @param email 输入待查email
     * @return  返回查询结果
     * @throws Exception    因为数据库操作可能出错,所以声明异常,调用的人需要处理异常或将异常继续抛出
     */
    @Override
    public ItripUser queryItripUserByEmail(@Param("email") String email) throws Exception {
        return itripUserMapper.queryItripUserByEmail(email);
    }

    /**
     * 邮箱注册新用户
     * @param itripUserVO 前端传来参数
     * @return 返回受影响的行数
     * @throws Exception 抛出异常
     */
    @Override
    public int addItripUserInfo(ItripUserVO itripUserVO) throws Exception{
        ItripUser itripUser = new ItripUser();
        itripUser.setUserCode(itripUserVO.getUserCode());
        itripUser.setUserPassword(MD5.getMd5(itripUserVO.getUserPassword(),32));
        itripUser.setUserName(itripUserVO.getUserName());
        String mailTo = itripUserVO.getUserCode();
        String random = UUID.randomUUID().toString().substring(0,5);
        mailService.sendSimpleMail(mailTo,random);
        int row = itripUserMapper.addItripUserInfo(itripUser);
        return row;
    }

    /**
     * 激活注册用户的邮箱
     * @param email 邮箱
     * @param activityCode  激活码
     * @return  返回结果
     * @throws Exception
     */
    @Override
    public boolean updateItripUserActivated(String email,String activityCode) throws Exception {
        ItripUser itripUser = queryItripUserByEmail(email);
        if (itripUser != null){
            if (itripUser.getActivated() != 1){
                if (DataCacheUtil.map.get(email).equals(activityCode)){
                    itripUserMapper.updateItripUserActivated(email);
                    return true;
                }else{
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     *  验证用户登录
     * @param email 用户邮箱
     * @param pwd   密码
     * @return  返回用户
     * @throws Exception
     */
    @Override
    public ItripUser isLoginSuccess(String email, String pwd) throws Exception {
        //根据邮箱获取对象
        ItripUser itripUser = queryItripUserByEmail(email);
        if (itripUser !=null){//用户存在
            String password = itripUser.getUserPassword();
            String pwdMD5 = MD5.getMd5(pwd,32);
            Integer activated = itripUser.getActivated();
            if (password.equals(pwdMD5)){//验证密码
                if (activated == 1){//验证状态码
                    return itripUser;
                }
            }
        }
        return itripUser;
    }
}
